package com.baosight.sczx.zm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.IndexConfiger;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;

/**
 * 首页安全模版-隐患
 * <p>Title: ServiceZM94.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-11-7 上午05:33:57  郭淑芳  Create
 *</pre>
 */
public class ServiceZM94 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZM94</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-11-8上午02:33:58 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskSource", false));//隐患来源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskLevel", false));//隐患级别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.riskClassify", false));//隐患分类
		return outInfo;
	}
	
	/**
	 * 隐患查询
	 * <p>Function: RiskQuery</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-11-7上午05:35:43 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {

		Map para1 = new HashMap();
		Map para2 = new HashMap();
		Map para3 = new HashMap();
		String isMore = "yes";		// 判断前台显示更多按钮或者处理按钮
		if ("否".equals(IndexConfiger.getInstance().getIsLeader())) {
			String department = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
//			Object ext2 = Department.getInstance().getDepartmentMapById(department).get("ext2");
//			if(ext2 !=null)
//			{
//				if ("采煤".equals(ext2.toString()) || "掘进".equals(ext2.toString())) {
					para1.put("fdManageDepartmentId", department);
					para2.put("fdDepartmentId", department);
					para3.put("fdDepartmentId", department);
					isMore = "no";
//				}
//			}
		}

		para1.put("fdUserId", UserSession.getLoginName());
		para2.put("fdUserId", UserSession.getLoginName());
		para3.put("fdUserId", UserSession.getLoginName());

		para1.put("fdStatus","未处理");
		para2.put("fdStatus","未整改");
		para3.put("fdStatus","未复查");
		
		para1.put("rownum","10");
		para2.put("rownum","10");
		para3.put("rownum","10");
		
		EiInfo outInfo = new EiInfo();
		
		// 未处理隐患查询
		outInfo.addBlock("untreatedResult").addBlockMeta(initMetaData());
		List<Map> untreatedList = dao.query("ZM94.queryForIndexRisk", para1);
		outInfo.addRows("untreatedResult", getPenalty(untreatedList));
		outInfo.set("num1", untreatedList.size());
		
		// 未反馈隐患查询
		outInfo.addBlock("noFeedback").addBlockMeta(initMetaData());
		List<Map> noFeedbackList = dao.query("ZM94.queryForIndexRisk", para2);
		outInfo.addRows("noFeedback", getPenalty(noFeedbackList));
		outInfo.set("num2", noFeedbackList.size());

		
		// 未复查隐患查询

		List<Map> noReviewList = dao.query("ZM94.queryForIndexRisk", para3);
		outInfo.addBlock("noReview").addBlockMeta(initMetaData());
		outInfo.addRows("noReview", getPenalty(noReviewList));
		outInfo.set("num3", noReviewList.size());
		
		outInfo.set("isMore", isMore);
		return outInfo;
	}

	/**
	 * <p>Function: getPenalty</p>
	 * <p>Description: 计算罚款金额 </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-21上午02:16:00 郭淑芳
	 * </p>
	 *
	*/
	private List<Map> getPenalty(List<Map> _list) {
		List<Map> retList = new ArrayList();
		
		for(int i=0; i<_list.size(); i++)
		{
			Map map = _list.get(i);
			// 计算该隐患的罚款金额
			String fdPenaltyIds = "";
			if(map.get("fdPenaltyIds") == null) 
			{
				map.put("fdPenalty", 0);
			}else
			{
				fdPenaltyIds = map.get("fdPenaltyIds").toString();
				String _temp[] = fdPenaltyIds.split(",");
				StringBuffer fdKeyIds = new StringBuffer();

				for(int j = 0; j < _temp.length; j++){
					if(j==0) {
						fdKeyIds.append("'"+_temp[j]+"'");
					}
					else {
						fdKeyIds.append(",'"+_temp[j]+"'");
					}
				}
				List<Map>  _listPenalty = new ArrayList();
				Map paraPenalty = new HashMap();
				paraPenalty.put("fdKeyIds", fdKeyIds.toString());
				_listPenalty = dao.query("ZMCFPenalty.getPenaltySum", paraPenalty);
				map.put("fdPenalty", _listPenalty.get(0).get("fdPenalty"));
			}
			retList.add(map);
		}
		return retList;
	}
	
	
	/**
	 * 获取三违各个状态的记录数
	 * <p>Function: getCount</p>
	 * <p>Description: </p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-12-17上午05:53:16 郭淑芳
	 * </p>
	 *
	 */
//	private int getCount(EiInfo info,Map paraForCount,String fdStatus)
//	{
//		int count =0;
//		paraForCount.put("fdStatus",fdStatus);	
//		System.out.println(paraForCount);
//		count = dao.query("ZM94.queryForIndexRisk",paraForCount).size();
//		return count;
//	}
	
	/**
	 * 初始化 EiBlockMeta
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlockMeta</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-11-19上午02:03:22 郭淑芳
	 * </p>
	 *
	 */
	public static EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();
		EiColumn eiColumn;
		
		eiColumn = new EiColumn("fdKeyId");
		eiColumn.setPrimaryKey(true);
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("主键");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskCode");
		eiColumn.setFieldLength(20);
		eiColumn.setDescName("隐患编号");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskDate");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("隐患日期");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdDepartment");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患单位");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdSourceId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患来源");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskLevelId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患级别");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskClassifyId");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("隐患分类");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdStatus");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("处理状态");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdDeadlineDate");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("解决期限");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdCheckPeople");
		eiColumn.setFieldLength(32);
		eiColumn.setDescName("检查人");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdPenalty");
		eiColumn.setType("N");
		eiColumn.setScaleLength(0);
		eiColumn.setFieldLength(15);
		eiColumn.setDescName("罚款金额");
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("fdRiskPlace");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("隐患地点");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdResponsibilityPeople");
		eiColumn.setFieldLength(200);
		eiColumn.setDescName("责任人");
		eiMetadata.addMeta(eiColumn);
				
		eiColumn = new EiColumn("fdRiskContent");
		eiColumn.setFieldLength(500);
		eiColumn.setDescName("隐患内容");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdName");
		eiColumn.setFieldLength(10);
		eiColumn.setDescName("隐患级别");
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}
}
